<!--企业资料信息-->
<template>
  <div class="app-container horizontal">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="资料名称" prop="fileName">
          <el-input
            v-model="filters.fileName"
            size="small"
            placeholder="请输入资料名称"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button id="Query" v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button id="ResetQuery" v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button icon="el-icon-plus" size="middle" @click="handleAdd">新增</el-button>
      <el-button icon="el-icon-edit" size="middle" :disabled="single" @click="handleUpdate">修改</el-button>
      <el-button icon="el-icon-delete" size="middle" :disabled="single" @click="handleDelete">删除</el-button>
      <el-button id="Detail" icon="el-icon-zoom-in" size="middle" :disabled="single" @click="handleDetail">详情</el-button>
      <el-col class="personal-type" :md="12" :lg="14" :xl="17">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </el-col>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column
        prop="fileName"
        label="资料名称"
        min-width="200"
        align="center"
        :show-overflow-tooltip="true"
      />
      <!--      <el-table-column-->
      <!--        prop="createBy"-->
      <!--        label="上传人"-->
      <!--        min-width="150"-->
      <!--        align="center"-->
      <!--      />-->
      <el-table-column
        label="上传时间"
        align="center"
        min-width="150"
      >
        <template slot-scope="{row}">
          <span v-if="row.appFiles && row.appFiles.length > 0">{{ row.appFiles[0].createTime }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 表格展示end-->
    <!-- 企业资料新增修改组件start -->
    <addDialog ref="addDialog" />
    <!-- 企业资料新增修改组件end -->

    <!--企业资料详情弹层start -->
    <detailDialog ref="detailDialog" />
    <!--企业资料详情弹层end -->
  </div>
</template>

<script>
import { listEnterpriseData, removeEnterpriseData } from '@/api/system/enterpriseData'
// 引入新增修改组件
import addDialog from './components/addDialog'
import detailDialog from './components/detailDialog.vue'
import { mapState } from 'vuex'
export default {
  components: {
    addDialog,
    detailDialog
  },
  data() {
    return {
      /* 搜索条件 start*/
      highLevelSet: false, // 高级设置显示隐藏
      filters: { // 查询参数
        pageNum: 1,
        pageSize: this.fristPageSize,
        fileName: '',
        createBy: ''
      },
      dateRange: [], // 日期范围
      /* 搜索条件 end*/

      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [] // 列表数据
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  watch: {
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    /* 列表 start*/
    // 获取列表
    getList() {
      this.loading = true

      listEnterpriseData(this.changeDateRange(this.filters, this.dateRange)).then(resp => {
        this.loading = false
        if (resp) {
          this.tableData = resp.data.rows
          this.total = resp.data.total
          this.addTaleData()
        } else {
          this.msgError(resp.message)
        }
      })
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        var arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /* 列表 end*/

    /* 按钮 start */
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    // 新增
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增企业资料')
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('企业资料详情', this.multipleSelection[0].id)
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改企业资料', this.multipleSelection[0].id)
    },
    // 删除
    handleDelete() {
      this.$confirm('确认要删除此企业资料吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.loading = true
          const id = this.multipleSelection[0].id
          removeEnterpriseData(id).then(res => {
            this.loading = false
            if (res) {
              this.msgSuccess('删除成功！')
              // 重新加载表格数据
              this.getList()
            } else {
              this.msgError(res.message)
            }
          })
        })
        .catch(() => {
          this.msgWarning('已取消删除')
        })
    }
  }
  /* 按钮 end */
}
</script>

